<?php
require_once('includes/common.php');
$lastPage = $_SERVER['HTTP_REFERER'];

$id = isset($_POST['id'])?$_POST['id']:0;

if($_FILES['edit_avatar']['error']==0){
  if($_FILES['edit_avatar']['size'] >= 2*1024*1024) {
    $_SESSION['msg'] = ('<span class="errMsg">Image size shuld not exceed 2MB</span>');
    header("location:$lastPage");exit;
  }

  /// Process to upload file///
  $source = $_FILES['edit_avatar']['tmp_name'];

  $fileName = date('YmdHis').'_'.$_FILES['edit_avatar']['name'];

  $dest = "uploads/$fileName";
  copy($source, $dest);
  $_POST['avatar'] = $fileName;
  ////////////// Generate THUMB///////////////////
  require_once('thumbnail/thumb.php');
  $lastAvatar = $_POST['tmp_avatar'];
  unset($_POST['tmp_avatar']);// we don't need that value in insert DB

  $img->GenerateThumbFile($dest, "uploads/thumbs/$fileName");
  //pr($_POST['tmp_avatar']);exit;
  unlink("uploads/thumbs/$lastAvatar");
  unlink("uploads/$lastAvatar");
  ///////////////////////////////
}
//// Pre process to handle submitted values////////
$_POST['lang'] = implode(',',$_POST['lang']);
$_POST['hobbies'] = implode(',',$_POST['hobbies']);


$updateQuery = "UPDATE `users` SET ";
foreach($_POST as $colName=>$value) {
  $updateQuery .="`$colName`='$value',";
}
$updateQuery = trim($updateQuery,', ');

$updateQuery .=" WHERE `id`=$id" ;

//echo $updateQuery;

$result = mysql_query($updateQuery) or die(mysql_error());

// to check how many records we inserted
//pr( mysql_affected_rows());
header('location:my_profile.php');






